

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dataMgr.Group;
import dataMgr.GroupManager;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
System.out.println("%555555555555555555555");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			System.out.println("hi hi");
			response.setContentType("text/plain");
			Group group ;
			
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			Session session = sessionFactory.openSession();

			Query query = session.createQuery("select max(g.groupID) from Group g ");

			java.util.List <Long>list = query.list();
			System.out.println(list.size());
			Long max;
			if(list==null || list.isEmpty())
				max=(long) 0;
			else
				max= list.get(0);
				
			
			System.out.println(max);
			if(max==null)
				max=(long) 0;
			
			Group group2=new Group();
		    String username=request.getParameter("username");

			group2.setUserName(username);
			group2.setGroupID(++max);
			

			GroupManager manager = new GroupManager(session);

			 manager.saveGroup(group2);

			 session.flush();
		   
		
		PrintWriter out =new PrintWriter(response.getOutputStream());
	//	  out.write((int) max);
		
		  out.print(max.toString());
		  out.flush();
		out.close();

		
	}


}
